

Imports System.IO

Imports System.Drawing.Imaging

 

Public Class Form1

    Inherits System.Windows.Forms.Form

    '/ <summary>

    '/ Required designer variable.

    '/ </summary>

    Private components As System.ComponentModel.Container = Nothing

 

    Private _tiffImage As Image

 

 

    Public Sub New()

        '

        ' Required for Windows Form Designer support

        '

        InitializeComponent()

 

        '

        ' TODO: Add any constructor code after InitializeComponent call

        '

        Me.SetStyle(ControlStyles.ResizeRedraw, True)

    End Sub 'New

 

 

    '/ <summary>

    '/ Clean up any resources being used.

    '/ </summary>

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing Then

            If Not (components Is Nothing) Then

                components.Dispose()

            End If

        End If

        MyBase.Dispose(disposing)

    End Sub 'Dispose

 

#Region "Windows Form Designer generated code"

 

    '/ <summary>

    '/ Required method for Designer support - do not modify

    '/ the contents of this method with the code editor.

    '/ </summary>

    Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.SuspendLayout()
        '
        'Form1
        '
        Me.AllowDrop = True
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(288, 173)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "Form1"
        Me.Text = "Generatore Multi Page Tiff"
        Me.ResumeLayout(False)

    End Sub 'InitializeComponent

 

#End Region

 

 

    '/ <summary>

    '/ The main entry point for the application.

    '/ </summary>

    <STAThread()>Shared Sub Main()

        Application.Run(New Form1)

    End Sub 'Main

 

 

    Protected Overrides Sub OnDragEnter(ByVal e As DragEventArgs)

        If e.Data.GetDataPresent(DataFormats.FileDrop) Then

            Dim allow As Boolean = True

            Dim s As String

            For Each s In CType(e.Data.GetData(DataFormats.FileDrop), String())

                Dim ext As String = Path.GetExtension(s).ToLower()

                If Not (ext = ".jpg" OrElse ext = ".jpeg" OrElse ext = ".tif" OrElse ext = ".tiff" OrElse ext = ".gif" OrElse ext = ".bmp") Then

                    allow = False

                End If

            Next s

            If allow Then

                e.Effect = DragDropEffects.Copy

            Else

                e.Effect = DragDropEffects.None

            End If

        End If

    End Sub 'OnDragEnter

 

 

    Protected Overrides Sub OnDragDrop(ByVal e As DragEventArgs)

        Dim sa As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())

 

        Dim dlg As New SaveFileDialog

        dlg.DefaultExt = ".tif"

        dlg.FileName = "unione"

        If dlg.ShowDialog() <> Windows.Forms.DialogResult.OK Then

            Return

        End If

 

        'get the codec for tiff files

        Dim info As ImageCodecInfo = Nothing

        Dim ice As ImageCodecInfo

        For Each ice In ImageCodecInfo.GetImageEncoders()

            If ice.MimeType = "image/tiff" Then

                info = ice

            End If

        Next ice 'use the save encoder

        Dim enc As Encoder = Encoder.SaveFlag

 

        Dim ep As New EncoderParameters(1)

        ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.MultiFrame))

 

        Dim pages As Bitmap = Nothing

 

        Dim frame As Integer = 0

        Dim s As String

        For Each s In sa

            If frame = 0 Then

                pages = CType(Image.FromFile(s), Bitmap)

                'save the first frame

                pages.Save(dlg.FileName, info, ep)

            Else

                'save the intermediate frames

                ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.FrameDimensionPage))

                Dim bm As Bitmap = CType(Image.FromFile(s), Bitmap)

                pages.SaveAdd(bm, ep)

            End If

 

            If frame = sa.Length - 1 Then

                'flush and close.

                ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.Flush))

                pages.SaveAdd(ep)

            End If

 

            frame += 1

        Next s

    End Sub 'OnDragDrop

 

  

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        Dim sf As StringFormat = CType(StringFormat.GenericTypographic.Clone(), StringFormat)

        sf.Alignment = StringAlignment.Center

        sf.LineAlignment = StringAlignment.Center

        e.Graphics.DrawString("Trascina le immagini qui per unirle in un unico Tiff (multipagina)", Font, Brushes.Black, New RectangleF(0, 0, Me.ClientSize.Width, Me.ClientSize.Height), sf)

    End Sub 'Form1_Paint

End Class 'Form1
